<template>
  <div style="width:500px;">
 <el-form
        ref="tform"
        :rules="Rules"
        :model='dataForm'
        label-width="80px"
        style="margin-top:20px;"
      >
    <el-col :span="12">
       <el-form-item label="姓名" prop="name">
          <el-input v-model="dataForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
           <el-form-item label="性别">
  <el-select v-model="dataForm.sex" placeholder="请选择">
    <el-option
      v-for="item in sexOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>     
     </el-form-item>
           <el-form-item label="年龄" prop="age">
          <el-input v-model.number="dataForm.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="职务">
 <el-select v-model="dataForm.position_id" filterable placeholder="请选择职务">
            <el-option
              v-for="item in positions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>     
        </el-form-item>
         <el-form-item label="部门">
          <el-select v-model="dataForm.depart_id" filterable placeholder="请选择部门">
            <el-option
              v-for="item in departList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>     
             </el-form-item>
              <el-form-item label="工作类别">
      <el-select v-model="dataForm.piece" placeholder="请选择计时或者计件">
    <el-option
      v-for="item in pieceOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>     
     </el-form-item>

    </el-col>
    <el-col :span="12">

        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model.number="dataForm.phone"
            placeholder="请输入员工手机号"
          ></el-input>
        </el-form-item>
 <el-form-item label="身份证号" prop="idcard">
          <el-input v-model="dataForm.idcard" placeholder="请输入身份证号"></el-input>
        </el-form-item>

        <el-form-item label="银行卡号" prop="banknum">
          <el-input v-model.number="dataForm.banknum" placeholder="请输入银行卡号"></el-input>
        </el-form-item>

        <el-form-item label="基本工资" prop="salary">
          <el-input v-model.number="dataForm.salary" placeholder="请输入基本工资"></el-input>
        </el-form-item>
        <el-form-item label="入职时间">
          <el-date-picker
            v-model="dataForm.hiredate"
            format="yyyy 年 MM 月 dd 日"
            value-format="timestamp"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工作状态">
        <el-select v-model="dataForm.status" placeholder="在职/离职">
    <el-option
      v-for="item in statusOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select> 
        </el-form-item>

    </el-col>
       
       
      </el-form>
 
  
 

  </div>
</template>

<script>
import headTop from "@/components/headTop";
import { insertStaff,updateStaff } from "@/api/getData";
export default {
  inject: ["reload"],
  data() {
    return {
    
      sexOptions:[{
          value: 0,
          label: '男'
        }, {
          value: 1,
          label: '女'
        }],
        pieceOptions:[
          {
          value: 0,
          label: '计时'
        }, {
          value: 1,
          label: '计件'
        }
        ],
        statusOptions:[
             {
          value: 0,
          label: '离职'
        }, {
          value: 1,
          label: '在职'
        }
        ],
        isEdit:false,
      Rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {type: 'number', message: '手机号必须为数字值'}
        ],
        age:[
          {type: 'number', message: '年龄必须为数字值'}
        ],
        salary:[
          {type: 'number', message: '基本工资填10的倍数'}
        ],
        hiredate: [
          { required: true, message: "请选择入职时间", trigger: "blur" }
        ]
      },
    };
  },
  created(){
    if (!this.isEdit) {
      this.dataForm.status = 1;
      this.dataForm.piece = 1;

    }
  },
  props:['dataForm','departList','positions'],
  components: {
  
  },
  methods: {
    onInsert() {
      this.isEdit = false;
      this.vilateForm();
    },
    onUpdate(){
      this.isEdit = true;
      this.vilateForm();
    },
    vilateForm(){
        this.$refs['tform'].validate(async valid => {
        if (valid) {
          if (this.isEdit) {
           this.updatestaffs();
          }else{
            this.addstaffs();

          }
        } else {
          this.$notify.error({
            title: "错误",
            message: "请检查输入是否正确",
            offset: 100
          });
          return false;
        }
      });
    },
    resetFields(){
      this.$refs['tform'].resetFields();
    },
    async addstaffs() {
      try {
        let params = this.dataForm;
        const result = await insertStaff(params);
        if (result.code == 200) {
          this.$message("添加员工成功");
          this.$emit('insertSuccess')
        } else {
          throw new Error("获取数据失败");
        }
      } catch (err) {
        console.log("获取数据失败", err);
      }
    },
    async updatestaffs(){
      let params = this.dataForm;
      updateStaff(params).then((res) => {
          if (res.code == 200){
          this.$message("修改员工成功");
          this.$emit('insertSuccess')
          }
        })
        .catch((err) => {

        });
    },
    changeSwitch(data){
      console.log(data);
    }
  }
};
</script>

<style lang="less" scoped>


</style>
